<template>
	<view class="wrap">
		<u-navbar title="修改交易密码" title-width="340" :border-bottom="false" back-icon-color="#fff" title-color="#fff" :background="{ backgroundColor: '#0030A8' }"></u-navbar>
		<view class="tom-container">
			<view class="list">
				<view class="row">
					<view class="row-label">账号</view>
					<view class="row-main">
						<input type="text" v-model="phone" maxlength="40" placeholder="请输入邮箱" placeholder-class="place-class" @blur="VerifyName"/>
					</view>
				</view>
				<view class="row">
					<view class="row-label">验证码</view>
					<view class="row-main">
						<input type="text" v-model="verificationCode" maxlength="6" placeholder="请输入验证码" placeholder-class="place-class" @blur="VerifyCode"/>
						<text @tap="getCode" class="row-main-tip">{{tips}}</text>
					</view>
				</view>
				<view class="row">
					<view class="row-label">新密码</view>
					<view class="row-main">
						<input type="password" v-model="transferPassword" maxlength="6" placeholder="请输入新密码" placeholder-class="place-class"/>
					</view>
				</view>
				<view class="row">
					<view class="row-label">确认新密码</view>
					<view class="row-main">
						<input type="password" v-model="confirmTransferPassword" maxlength="6" placeholder="请确认新密码" placeholder-class="place-class"/>
					</view>
				</view>
				<view class="login_btn" @tap="submit">确认修改</view>
			</view>
		</view>
		<u-verification-code :seconds="seconds" ref="uCode" @change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:'',// 手机号码
				verificationCode:'',// 验证码
				transferPassword:'',// 新密码
				confirmTransferPassword:'',//确认新密码
				tips:'获取验证码',//倒计时提示文字
				seconds:60,
				numberType:0,/*账号类型，1手机2邮箱*/
			}
		},
		methods: {
			VerifyName(e){
				this.phone = e.target.value;
			},
			VerifyCode(e){
				this.verificationCode = e.target.value;
			},
			// 确定
			submit(){
				// if(!this.$u.test.mobile(this.phone)) return this.$utils.toast('请输入正确的手机号');
				// if(!this.$u.test.mobile(this.phone)){
					if(!this.$u.test.email(this.phone)){
						return this.$utils.toast('请输入正确的邮箱')
					}
				// }
				if(this.$u.test.isEmpty(this.verificationCode)) return this.$utils.toast('请输入验证码');
				if(!this.$u.test.digits(this.transferPassword) || this.transferPassword.toString().length!=6) return this.$utils.toast('请输入6位数字密码');
				if(this.transferPassword.toUpperCase()!=this.confirmTransferPassword.toUpperCase()) return this.$utils.toast('两次输入密码不一致')
				this.$utils.loading('请稍后...')
				this.$api.loginApi.changeTransactionPassword({
					phone:this.phone,
					verificationCode:this.verificationCode,
					newTransferPassword:this.transferPassword
				})
				.then(res=>{
					this.$utils.toast(res.data)
					uni.navigateBack()
				})
			},
			// 倒计时变化
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				// if(!this.$u.test.mobile(this.phone)) return this.$utils.toast('请输入正确的手机号');
				// if(!this.$u.test.mobile(this.phone)){
					if(!this.$u.test.email(this.phone)){
						return this.$utils.toast('请输入正确的账号')
					}
				// }
				if(this.$u.test.mobile(this.phone)) this.numberType = 1;
				if(this.$u.test.email(this.phone)) this.numberType = 2;
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					// if(this.numberType == 1){
						// this.$api.loginApi.getMsg({
						// 	phone:this.phone
						// })
						// .then(res=>{
						// 	uni.hideLoading();
						// 	// 这里此提示会被this.start()方法中的提示覆盖
						// 	this.$utils.toast('验证码已发送');
						// 	// 通知验证码组件内部开始倒计时
						// 	this.$refs.uCode.start();
						// })
					// }else if(this.numberType == 2){
						this.$api.loginApi.sendMailVerificationCode({
							mailAccount:this.phone
						})
						.then(res=>{
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$utils.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						})
					// }
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.tom-container {
	padding-top: 50rpx;
}
.list {
	flex: 1;
	display: flex;
	flex-direction: column;
	border-radius: 50rpx 50rpx 0rpx 0rpx;
	overflow: hidden;
	background-color: #fff;
	background-image: url('/static/new_images/showPopup.png');
	background-position: left bottom;
	background-repeat: no-repeat;
	background-size: 100% 800rpx;
	.row {
		margin: 30rpx;
	
		&:first-child {
			margin-top: 40rpx;
		}
	
		&-label {
			color: $tom-text-color;
			font-size: 28rpx;
			font-weight: bold;
		}
	
		&-main {
			margin-top: 28rpx;
			background-color: #FAFAFA;
			height: 68rpx;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
	
			.place-class {
				color: #CCCCCC;
			}
	
			input {
				flex: 1;
				color: $tom-text-color;
				font-size: 24rpx;
			}
	
			&-tip {
				margin-left: 20rpx;
				color: $tom-text-color;
				font-size: 24rpx;
			}
		}
	}
}
</style>
